<?php

/*
	活码列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['types']=$this->types;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']);
	//类型。1表示开发活码，2表示bug修复活码，3表示客户异常问题处理
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	//状态。0表示待处理。1表示处理中。2表示处理完毕待审核。3表示审核通过。
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//活码名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_worker_ewm_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$row['url']=custom_url('jy_worker_ewm/item//id='.$row['id'].'&onemerchid='.$_SESSION['onemerchid'],true);
		$workerid=pdo_getcolumn('jy_worker_ewm_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'workerid');
		$row['worker']=pdo_getcolumn('jy_scrm_worker',array('id'=>$workerid,'uniacid'=>$_SESSION['uniacid']),'name');
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活码列表'),
			array('op'=>'edit','title'=>'添加活码'),
			array('op'=>'editlist','title'=>'根据桌台批量生产活码'),
		),
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：
		</br>1、这里添加的目标链接+绑定企业微信员工，会生成一个活码链接，可以复制这【活码链接】，去印刷二维码。后面修改【目标链接】，也不会影响本活码二维码的使用！
		</br>2、绑定企业微信员工后，设置生成加好友二维码。前端就会判断是否已加好友，未加好友，就会跳转到加好友的页面；
		</br>3、加企业微信好友后，就会自动弹窗欢迎语句，需要在不同的员工页面设置欢迎语句，并添加跳转链接（比如点餐的：就添加点餐的链接。）
		</br>4、<red>企业微信客户跟公众号粉丝互通，才能锁定跳转！</red>，需要开启微信三方开放平台：https://open.weixin.qq.com/，<red>如何设置？如何绑定微信开发者ID?</red>：https://developer.work.weixin.qq.com/document/path/92114#%E5%A6%82%E4%BD%95%E7%BB%91%E5%AE%9A%E5%BE%AE%E4%BF%A1%E5%BC%80%E5%8F%91%E8%80%85id

		','delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'活码名称','type'=>'string'),
			array('name'=>'url','title'=>'活码二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'活码链接','type'=>'copy'),
			array('name'=>'worker','title'=>'关联员工','type'=>'string'),
			array('name'=>'ewm','title'=>'好友二维码','type'=>'image'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'getewm','title'=>'刷新企微加好友二维码'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'活码名称'),
		),
	);
	$return['htmls']=$htmls;
	$return['workers']=pdo_getall('jy_worker_ewm_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('id','workerid'),'id');
	foreach($return['workers'] as &$wv){
		$wv['title']=pdo_getcolumn('jy_scrm_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
	}
	exi($return);
}
//添加编辑活码
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['title']){
			exi('请填写标题！','error');
		}
		if(!$post['workerid']){
			exi('请选择员工！','error');
		}
		$data=array(
			'title'=>trim($post['title']),
			'desc'=>trim($post['desc']),
			'gameid'=>intval($post['gameid']),
			'workerid'=>intval($post['workerid']),
			'image'=>trim($post['image']),
			'linktitle'=>trim($post['linktitle']),
			'linkdesc'=>trim($post['linkdesc']),
			'linkimg'=>trim($post['linkimg']),
			'link'=>trim($post['link']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_worker_ewm_list',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			pdo_insert('jy_worker_ewm_list',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_worker_ewm_list',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回活码列表'),
		),
		'desc'=>'设置活码信息',
		'edit'=>array(
			array('name'=>'title','title'=>'活码名称','type'=>'string'),
			array('name'=>'desc','title'=>'活码描述','type'=>'text'),
			array('name'=>'image','title'=>'活码图片','type'=>'image'),
			array('name'=>'linktitle','title'=>'链接名称','type'=>'string','help'=>'如果不填就不弹出；客户扫活码加本员工好友后，会自动弹出【跳转链接】相关信息，方便客户自助点击进入真实的链接地址！'),
			array('name'=>'linkdesc','title'=>'链接描述','type'=>'string'),
			array('name'=>'linkimg','title'=>'链接图片','type'=>'image'),
			array('name'=>'link','title'=>'原始链接','type'=>'link','from'=>'action'),
			array('type'=>'choose','title'=>'所属员工','name'=>'workerid','action'=>'jy_worker_ewm/merch.get_worker/'),
		),
	);
	exi($return);
}
if($_RQ['op']=='editlist'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['title']){
			exi('请填写活码标题后缀！','error');
		}
		if(!$post['workerid']){
			exi('请选择员工！','error');
		}
		$list=pdo_getall('jy_weishop_table',array('status'=>1,'merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
		foreach($list as $item){
			$link='jy_merch/myshop//scanning=1&id='.$_SESSION['onemerchid'].'&tableid='.$item['id'];
			$data=array(
				'title'=>$item['title'].'-'.trim($post['title']),
				'desc'=>trim($post['desc']),
				'workerid'=>intval($post['workerid']),
				'image'=>trim($post['image']),
				'link'=>$link,
			);
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			pdo_insert('jy_worker_ewm_list',$data);
		}
		exi('操作成功！','','list');
	}
	$item=array();
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回活码列表'),
		),
		'desc'=>'设置活码信息',
		'edit'=>array(
			array('name'=>'title','title'=>'活码标题后缀','type'=>'string','help'=>''),
			array('name'=>'desc','title'=>'活码描述','type'=>'text'),
			array('name'=>'image','title'=>'活码图片','type'=>'image'),
			array('type'=>'choose','title'=>'所属员工','name'=>'workerid','action'=>'jy_worker_ewm/merch.get_worker/'),
		),
	);
	exi($return);
}
if($_RQ['op']=='getewm'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_worker_ewm_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$worker=pdo_get('jy_scrm_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),array('userid','name'));
		$item['ewm']=model('jy_scrm/worker')->get_ewm($worker['userid'],$worker['name'],'jy_scrm|jy_worker_ewm|'.$item['id']);
		if($item['ewm']){
			pdo_update('jy_worker_ewm_list',array('ewm'=>$item['ewm']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		}
	}else{
		exi('请选择员工！','error');
	}
	exi('生成成功！','','list');
}